Your first action - Hello WebWorld

TODO: finish this.

The Code

com.acme.action.HelloWorld.java
package com.acme.test;

import com.opensymphony.xwork.ActionSupport;

public class HelloWorld extends ActionSupport
{
	private String message;

	public String execute()
	{
		message = "Hello, WebWorld!";
		return SUCCESS;
	}
	
	public String getMessage()
	{
		return message;
	}
}

Let us create the view page. Create a file hello.jsp and place it under the YOUR_WEBAPP/WEB-INF/jsp directory:

hello.jsp
<%@ taglib prefix="ww" uri="/webwork" %>
 <html>
   <head>
    <title>Hello Page</title>
   </head>
  <body>
    WebWork says:
    <h1><ww:property value="message"/></h1>
  </body>
 </html>

Edit the WEB-INF/classes/xwork.xml file as shown below, adding the helloWorld action and something called an interceptor to the default package. Read more: xwork.xml

xwork.xml
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
  <xwork>
   <!-- Include webwork defaults (from WebWork JAR). -->
   <include file="webwork-default.xml"/>

   <package name="default" extends="webwork-default">
     <default-interceptor-ref name="basicStack"/>

      <action name="helloWorld" class="com.acme.test.HelloWorld">
         <result name="success">/WEB-INF/jsp/hello.jsp</result>
      </action>
   </package>
  </xwork>

Go ahead and try it now: go to the url http://localhost:8080/YOUR_WEBAPP/helloWorld.action and see what happens. You should see a page that says "Hello, WebWorld!".

How the code worksm

The above four files work together like this.

  • The WebWork filter (mapped on /*, meaning every request) receives the request for helloWorld.action.
  • WebWork passes the request on to its ActionMapper, and lets this ActionMapper handle the request for a helloWorld action.
  • The ActionMapper finds the class registered with the helloWorld name in the xwork.xml.
  • By default, the execute() method gets called (but you can override this).
  • execute() returns a String called SUCCESS, and WebWork looks again in xwork.xml to what and if any result is registered with that name. It finds the result (by default, a ServletDispatcher), which maps to /WEB-INF/jsp/hello.jsp.
  • The page hello.jsp is processed (the <ww:property value="message" /> tag then uses OGNL to call the getter getMessage() of the HelloWorld.java action) and sent back to the browser.